<template>
    <Layout>
        <div class="navBar">
            <Icon class="leftIcon" iconname="left" @click="goBack"/>
            <span class="title">编辑标签</span>
            <span class="rightIcon"></span>
        </div>
        <div class="form-wrapper">
            <FormItem :value="tag.name" field-name="标签名" placeholder="请输入标签名" @update:value="update"/>
        </div>
        <div class="button-wrapper">
            <Button @click="removeTag">删除标签</Button>
        </div>
    </Layout>
</template>

<script lang="ts">
    import Vue from 'vue';
    import {Component} from 'vue-property-decorator';
    import Button from '@/components/Button.vue';
    import FormItem from '@/components/Money/FormItem.vue';
    import store from '@/store/index2.ts';

    @Component({
        components: {Button, FormItem}
    })
    export default class EditLabel extends Vue {
        // id = this.$route.params.id

        // eslint-disable-next-line no-undef
        get tag() {
            return this.$store.state.currentTag;
        }

        update(name: string) {
            //接收的name是@update:value传来的
            if (this.tag) {
                this.$store.commit('updateTag', {id: this.tag.id, name: name});

            }
        }

        removeTag() {
            if (this.tag) {
                this.$store.commit('removeTag',this.$route.params.id);

            }
        }

        goBack() {
            this.$router.replace('/labels');
        }

        created() {
            //寻找缓存中的存储的tag是否存在，不存在替换路径为/404,路由的index里定义了为其他路径时响应notFound页面
            const id = this.$route.params.id;
            this.$store.commit('fetchTags');
            this.$store.commit('setCurrentTag', id);
            if (!this.tag) {
                this.$router.replace('/404');
            }
        }
    }
</script>

<style lang="scss" scoped>
    .navBar {
        text-align: center;
        font-size: 16px;
        padding: 12px 16px;
        background: white;
        display: flex;
        align-items: center;
        justify-content: space-between;

        > .title {
        }

        > .leftIcon {
            width: 24px;
            height: 24px;
        }

        > .rightIcon {
            width: 24px;
            height: 24px;
        }
    }

    .form-wrapper {
        background: white;
        margin-top: 8px;
    }

    .button-wrapper {
        text-align: center;
        padding: 16px;
        margin-top: 44-16px;
    }
</style>